<?

/**
* Use $_SESSION["GDate"] if you are using custom date
* or just use the system date
*
* $today_date = $_SESSION["GDate"];
* $today_date = date("d-m-Y");
*
*/
$today_date		  = date("d-m-Y");

$thisSequence   = "PCH_SEQ";
$txType         = _PEMBELIAN;

$srcDbSequence      = "MST_SEQUENCE";
$srcDbHdr           = "PCH_HDR";
$srcDbHdrLog        = "LOG_PCH_HDR";
$srcDbDtl           = "PCH_DTL";
$srcDbSubDtl        = "PCH_SUB_DTL";
$srcDbMstModel      = "MST_MODEL";
$srcDbInputHistory  = "MODEL_INPUT_HISTORY";
$srcDbSupplier      = "SUPPLIER";

$UID     = unserialize($_SESSION["serUID"]);
$userId  = $UID->getUID();

$CHandlers = new CGHandlers;
$CFetchers = new CGFetchers;

$COracle = new oracleDb;
$COracle->init($_SESSION["ORCL"],
               $_SESSION["OPASS"],
               $_SESSION["SID"],
               _NO_COMMENT);

if($COracle->connect()){

	/**
	* Delete the previous entry
	*
	*/
	$CHandlers->dbEntDelete($COracle,
            	            $_SESSION["jid"],
            	            $srcDbHdr);

  $CHandlers->dbEntDelete($COracle,
            	            $_SESSION["jid"],
            	            $srcDbDtl);

	$CHandlers->dbEntDelete($COracle,
            	            $_SESSION["jid"],
            	            $srcDbSubDtl);

	$CHandlers->dbEntDelete($COracle,
            	            $_SESSION["jid"],
            	            $srcDbInputHistory);

  $CHandlers->dbEntDelete($COracle,
                          $_SESSION["jid"],
                          $SourceDb->fetchValue("srcDbDtlDp"));

  $CHandlers->dbEntDelete($COracle,
                          $_SESSION["jid"],
                          $SourceDb->fetchValue("srcDbSubDp"));

  /**
  * We don't build up any jobId anymore since this is
  * a modify mode. Also we already have the previous
  * jobId in $_SESSION
  *
  */
  $jobId = $_SESSION["jid"];

  $hdrDate = "TO_DATE('".$_POST["trDateEnt"]."' , 'dd-mm-YYYY')";
  $dueDate = "TO_DATE('".$_POST["dueDateEnt"]."', 'dd-mm-YYYY')";

  /**
  * Insert all data
  *
  */
  for($iRow = 0; $iRow < $dataup; $iRow++){

    $CFetchers->fetchModelId($COracle,
                             $_POST["modelEnt"][$iRow],
                             $srcDbMstModel,
                             $modelId);

    $values = " '".$jobId."', ".                       /* Job id     */
              " '".$_POST["whsEnt"][$iRow]."', ".      /* Warehouse    */
              " '".$_POST["modelEnt"][$iRow]."' , ".   /* model      */
              " '".$modelId."', ".                     /* Model Id   */
              " '".$_POST["cvtEnt"][$iRow]."' , ".     /* Converted to */
              "  ".$_POST["cvtQtyEnt"][$iRow]." , ".   /* Converted Qty*/
              " '".$_POST["unitEnt"][$iRow]."' , ".    /* Unit       */
              "  ".$_POST["qtyEnt"][$iRow]." , ".      /* quantity   */
              "  ".$_POST["priceEnt"][$iRow]." , ".    /* Model price */
              "  ".$_POST["hrzTotEnt"][$iRow].",  ".   /* horizontal total */
              "  ".$_POST["discPctgEnt"][$iRow]." , ". /* dicount percent  */
              "  ".$_POST["discEnt"][$iRow]." , ".     /* discount amount  */
              "  ".$_POST["netEnt"][$iRow]." , ".      /* Netto amount     */
              "  ".($iRow + 1)."  ";

    $CLHandlers->insToDbSubDtl($COracle,
                               $values,
                               $srcDbSubDtl);

    $valTx   =  " '".$jobId."', ".                       /* Job id     */
                "  ".$hdrDate." , ".                     /* Tr Date    */
                " '".$modelId."', ".                     /* Model Id   */
                "  ".$_POST["qtyEnt"][$iRow]." , ".      /* quantity   */
                "  ".$_POST["priceEnt"][$iRow]." , ".    /* Model price */
                "  ".$_POST["netEnt"][$iRow].",  ".      /* net total */
                "  ".($iRow + 1).", ".                   /* Source detail number */
                "  ".$txType."  ";                       /* Tx Type        */

    $CLHandlers->insTxHistory($COracle,
                              $valTx,
                              $srcDbInputHistory);
  }

  /**
  * Insert the totals into $srcDbDtl
  *
  */
  $values = " '".$jobId."', ".
            "  ".$_POST["totHrzTotEnt"].", ".  /* Total HrzTot     */
            "  ".$_POST["totDiscEnt"].", ".    /* Total discount   */
            "  ".$_POST["totNetEnt"]."  ";     /* Total Netto      */

  $CLHandlers->insToDbDtl($COracle,
                          $values,
                          $srcDbDtl);

  $CFetchers->fetchHdrNo($COracle,
                         $_POST["trDateEnt"],
                         $srcDbHdr,
                         $hdrNo);

  $values = " '".$jobId."', ".                  /* Job id */
            " ' ',    ".                        /* Reference id ' ' default */
            " '".$_POST["suppCstNameEnt"]."',". /* Supplier Name */
            " '".$_POST["suppCstCodeEnt"]."', "./* Supplier Code */
            "  ".$hdrDate." , ".                /* Tx date      */
            "  ".$dueDate.", ".                 /* Due Date     */
            " '".$_POST["currencyEnt"]."', ".   /* Currency Symbol */
            "  ".$_POST["rateEnt"].", ".        /* Rate         */
            "  ".$_POST["crTermEnt"].", ".      /* Creadit Term */
            " '".$_POST["notaSrcEnt"]."', ".    /* Nota source  */
            "  ".$_POST["totAmount"].", ".      /* Amount       */
            "  ".$_POST["totTaxPctg"].", ".     /* Tax percentage  */
            "  ".$_POST["totTax"].", ".         /* Tax ammount  */
            "  ".$_POST["gDiscPctg"].", ".      /* Disc percentage */
            "  ".$_POST["totGDisc"].", ".       /* Disc Amount     */
            "  ".$_POST["totExpense"].", ".     /* Expennse        */
            "  ".$_POST["totPrepaid"].", ".     /* Prepaid         */
            "  ".$_POST["totPayable"].", ".     /* payableEnt      */
            " '".$_POST["memoEnt"]."', ".       /* Memo            */
            "  ".$hdrNo.", ".                   /* Number          */
            " '"._NOT_ACTIVE."' ";              /* Status          */


  $CLHandlers->insToDbHdr($COracle,
                          $values,
                          $srcDbHdr);

  /**
   * transfer process
   * tmp --> real
   */
  $CLHandlers->transDpTmpToReal($COracle,
                                $jobId,
                                $_SESSION["tmpJid"],
                                $SourceDb);

  $CHandlers->dbEntDelete($COracle,
                          $_SESSION["tmpJid"],
                          $SourceDb->fetchValue("srcDbTmpDtlDp"));

  $CHandlers->dbEntDelete($COracle,
                          $_SESSION["tmpJid"],
                          $SourceDb->fetchValue("srcDbTmpSubDp"));

  /**
  * Insert Log
  *
  */
  $CHandlers->insToDbLog($COracle,
                         $today_date,
                         $jobId,
                         $userId,
                         $_SERVER["REMOTE_ADDR"],
                         _ENTRY_MODE,
                         $srcDbHdrLog);

	$COracle->commit();

	if($COracle->getStatus() == true){

	  $Misc->usrMsg("Modify Complete");
	}else{

	  $Misc->usrMsg("Modify Process Interupted");
	}

	$COracle->dc();

	/**
	* Pending...
	* I'm not sure if this
	* reset procedure off
	* causes buggy
	*
	*	$restore  = 0;
	* $uprun    = 0;
	* $dataup   = 0;
	*
	*/
	//jumpTo(2,"../".$_SESSION["source_caller"]);

}else{

	echo "<br/><font size='16'>Error reason: upload connection failed </font>\n";
}

?>